package org.liquidByte.trafficMustang.androidApp.listview.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class EventsAdapter extends BaseAdapter {
	private DeslocamentoRowAdapter deslocamentoAdapter;
	private TrechoRowAdapter trechoAdapter;
	private AceiteRowAdapter aceiteAdapter;
	private AvaliacaoRowAdapter avaliacaoAdapter;
	private MensagemRowAdapter mensagemAdapter;
	
	public EventsAdapter(Context context) {
		super();
		
		EventsChangedObserver observer = new EventsChangedObserver(this);
		
		this.deslocamentoAdapter = new DeslocamentoRowAdapter(context);
		this.deslocamentoAdapter.registerDataSetObserver(observer);
		
		this.trechoAdapter = new TrechoRowAdapter(context);
		this.trechoAdapter.registerDataSetObserver(observer);
		
		this.aceiteAdapter = new AceiteRowAdapter(context);
		this.aceiteAdapter.registerDataSetObserver(observer);
		
		this.avaliacaoAdapter = new AvaliacaoRowAdapter(context);
		this.avaliacaoAdapter.registerDataSetObserver(observer);
		
		this.mensagemAdapter = new MensagemRowAdapter(context);
		this.mensagemAdapter.registerDataSetObserver(observer);
	}

	@Override
	public int getCount() {
		return this.deslocamentoAdapter.getCount() +
			   this.trechoAdapter.getCount() +
			   this.aceiteAdapter.getCount() +
			   this.avaliacaoAdapter.getCount() + 
			   this.mensagemAdapter.getCount();
	}
	
	private AdapterRef resolveRef(int position) {
		int deslocamentoCriteria = this.deslocamentoAdapter.getCount();
		int trechoCriteria = this.trechoAdapter.getCount() + deslocamentoCriteria;
		int aceiteCriteria = this.aceiteAdapter.getCount() + trechoCriteria;
		int avaliacaoCriteria = this.avaliacaoAdapter.getCount() + aceiteCriteria;
		int mensagemCriteria = this.mensagemAdapter.getCount() + avaliacaoCriteria;
		
		if (position < deslocamentoCriteria) {
			 return new AdapterRef(this.deslocamentoAdapter, position);
		} else if (position < trechoCriteria) {
			return new AdapterRef(this.trechoAdapter, position - deslocamentoCriteria);
		} else if (position < aceiteCriteria) {
			return new AdapterRef(this.aceiteAdapter, position - trechoCriteria);
		} else if (position < avaliacaoCriteria) {
			return new AdapterRef(this.avaliacaoAdapter, position - aceiteCriteria);
		} else if (position < mensagemCriteria) {
			return new AdapterRef(this.mensagemAdapter, position - avaliacaoCriteria);
		} else {
			return null;
		}
	}

	@Override
	public Object getItem(int position) {
		AdapterRef ref = this.resolveRef(position);
		if (ref == null) {
			return null;
		} else {
			return ref.adapter.getItem(ref.relativePosition);
		}
	}

	@Override
	public long getItemId(int position) {
		AdapterRef ref = this.resolveRef(position);
		if (ref == null) {
			return 0;
		} else {
			return ref.adapter.getItemId(ref.relativePosition);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		AdapterRef ref = this.resolveRef(position);
		if (ref == null) {
			return null;
		} else {
			return ref.adapter.getView(ref.relativePosition, convertView, parent);
		}
	}
	
	public DeslocamentoRowAdapter getDeslocamentoAdapter() {
		return deslocamentoAdapter;
	}
	public TrechoRowAdapter getTrechoAdapter() {
		return trechoAdapter;
	}
	public AceiteRowAdapter getAceiteAdapter() {
		return aceiteAdapter;
	}
	public AvaliacaoRowAdapter getAvaliacaoAdapter() {
		return avaliacaoAdapter;
	}
	public MensagemRowAdapter getMensagemAdapter() {
		return mensagemAdapter;
	}

	public void clear() {
		this.deslocamentoAdapter.clear();
		this.avaliacaoAdapter.clear();
		this.trechoAdapter.clear();
		this.aceiteAdapter.clear();
		this.mensagemAdapter.clear();
	}
	
	private class AdapterRef {
		private BaseAdapter adapter;
		private int relativePosition;
		
		public AdapterRef(BaseAdapter adapter, int relativePosition) {
			super();
			this.adapter = adapter;
			this.relativePosition = relativePosition;
		}
	}
	
	private class EventsChangedObserver extends DataSetObserver {
		
		private EventsAdapter adapter;

		private EventsChangedObserver(EventsAdapter adapter) {
			super();
			this.adapter = adapter;
		}

		@Override
		public void onChanged() {
			super.onChanged();			
			this.adapter.notifyDataSetChanged();
		}

		@Override
		public void onInvalidated() {
			super.onInvalidated();			
			this.adapter.notifyDataSetInvalidated();
		}
		
	}
}
